package thread;

/**
 * 有效的缩小同步范围可以在保证并发安全的前提下尽可能的提高并发效率
 * 同步快：
 * synchronized() {
 *     代码块
 * }
 */
public class syncDemo2 {
    public static void main(String[] args) {
        Shop s1 = new Shop();
        Shop s2 = new Shop();
        Thread f = new Thread("范") {
            public void run() {
                s1.buy();
            }
        };
        Thread w = new Thread("王") {
            public void run() {
                s2.buy();
            }
        };
        f.start();
        w.start();
    }
}

class Shop {
    public void buy() {
        try {
            Thread t =  Thread.currentThread();
            System.out.println(t.getName() + "正在挑选衣服...");
            Thread.sleep(3000);
            synchronized (this) {
                System.out.println(t.getName() + "正在试衣服...");
                Thread.sleep(3000);
            }
            System.out.println(t.getName() + "结账离开");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}